package de.tabuma.pm.externalproject.controller;

import de.tabuma.lib.tabumacontroller;
import de.tabuma.pm.*;
import de.tabuma.pm.externalproject.entity.*;
import de.tabuma.pm.externalproject.logik.*;

import java.io.PrintWriter;
import java.util.*;
import java.util.logging.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.google.appengine.api.users.*;

/**
 *
 * @author alefen
 */
public class update extends tabumacontroller {

    private static final Logger log = Logger.getLogger(update.class.getName());

    public update(String servlet, Hashtable parameter) {
        super(servlet, parameter);
    }

    @Override
    public void navigate(ServletContext context, HttpServletRequest request, HttpServletResponse response, PrintWriter out) {
        String wrongUrl = "Url, Parameter or Name is not valid.";

        if(request.getParameter(constants.cancel)!=null) {
            list list = new list(this.getServlet(), this.getParameter());
            list.navigate(context, request, response, out);
            return;
        }

        UserService userService = UserServiceFactory.getUserService();
        User user = userService.getCurrentUser();

        String externProjectId = request.getParameter(constants.externalProjectId);
        int externProjectIdInt = Integer.valueOf(externProjectId).intValue();

        if(request.getParameter(constants.updateExternalProject)!=null) {

            ExternalProject ep = ExternalProjectFacade.get(externProjectIdInt);

            boolean first = true;
            String parameterList = "";
            List<HiddenParameter> hpList = HiddenParameterFacade.getList(externProjectIdInt);
            for(int i=0;i<hpList.size();i++) {
                if(!first) {
                    parameterList += ",";
                }
                HiddenParameter hp = hpList.get(i);
                parameterList += hp.getName()+"="+hp.getValue();
                first = false;
            }

            List<LoginUserParameter> lupList = LoginUserParameterFacade.getList(externProjectIdInt);
            for(int i=0;i<lupList.size();i++) {
                if(!first) {
                    parameterList += ",";
                }
                LoginUserParameter lup = lupList.get(i);
                parameterList += lup.getName()+"=$"+lup.getNameForUser();
                first = false;
            }

            request.setAttribute(constants.updateExternalProject, "true");
            request.setAttribute(constants.externalProject, ep);
            request.setAttribute(constants.externalProjectName, ep.getName());
            request.setAttribute(constants.externalProjectParameter, parameterList);
            request.setAttribute(constants.externalProjectUrl, ep.getLoginUrl());
            request.setAttribute(constants.externalProjectId, ""+ep.getId());
            return;
        }
        
        if(request.getParameter(constants.updateExternalProject1)!=null) {

            String url = request.getParameter(constants.externalProjectUrl);
            String projectName = request.getParameter(constants.externalProjectName);
            String projectParameter = request.getParameter(constants.externalProjectParameter);
            String projectPost = request.getParameter(constants.externalProjectPost);
            
            List<HiddenParameter> paramList1 = new ArrayList<HiddenParameter>();
            List<LoginUserParameter> paramList2 = new ArrayList<LoginUserParameter>();

            ExternalProject ep = ExternalProjectFacade.get(externProjectIdInt);

            try {

                String parameterList[] = projectParameter.split(",");
                if(parameterList.length<2) {
                    throw new Exception();
                }

                paramList1 = create.getHiddenParameterList(projectParameter);
                paramList2 = create.getLoginUserParameterList(projectParameter);

                ExternalProjectFacade.removeParameter(externProjectIdInt, user.getEmail());

                
                ep.setLoginUrl(url);
                ep.setName(projectName);
                if(projectPost.equals(constants.externalProjectPost_Post)) {
                    ep.setPost(Boolean.TRUE);
                }
                else {
                    ep.setPost(Boolean.FALSE);
                }
                ExternalProjectFacade.update(ep, user.getEmail());
                int id = ep.getId().intValue();
                for(int i=0;i<paramList1.size();i++) {
                    HiddenParameter hp = paramList1.get(i);
                    hp.setExternalprojectId(id);
                    HiddenParameterFacade.create(hp, user.getEmail());
                }
                for(int i=0;i<paramList2.size();i++) {
                    LoginUserParameter lup = paramList2.get(i);
                    lup.setExternalprojectId(id);
                    LoginUserParameterFacade.create(lup, user.getEmail());
                }

                list list = new list(this.getServlet(), this.getParameter());
                list.navigate(context, request, response, out);
            }
            catch(Exception e) {
                String warning = e.getMessage()+"/"+e.getCause()+"/"+e.getClass();
                for(int i=0;i<e.getStackTrace().length;i++) {
                    warning += "\n" + e.getStackTrace()[i];
                }
                log.warning(warning);

                request.setAttribute(constants.updateExternalProject, "true");
                request.setAttribute(constants.externalProject, ep);
                request.setAttribute(constants.externalProjectErrorMessage, wrongUrl);
                request.setAttribute(constants.externalProjectUrl, request.getParameter(constants.externalProjectUrl));
                request.setAttribute(constants.externalProjectName, request.getParameter(constants.externalProjectName));
                request.setAttribute(constants.externalProjectId, ""+externProjectId);
                request.setAttribute(constants.externalProjectParameter, request.getParameter(constants.externalProjectParameter));
            }
        }
    }
}